package org.career.pdung.DAOImpl;

import java.util.List;

import javax.annotation.Resource;
import org.career.pdung.DAO.PersonDAO;
import org.career.pdung.domain.Person;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

@SuppressWarnings("restriction")
public class PersonDAOImpl implements PersonDAO {
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	@SuppressWarnings("unchecked")
	public List<Person> getAll() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("FROM  Person");
		return  query.list();
	}
	public Person get( Integer id ) {
		Session session = sessionFactory.getCurrentSession();
		Person person = (Person) session.get(Person.class, id);		
		return person;
	}
	public void add(Person person) {
		
		Session session = sessionFactory.getCurrentSession();
		session.save(person);
	}
	public void delete(Integer id) {
		
		Session session = sessionFactory.getCurrentSession();
		Person person = (Person) session.get(Person.class, id);
		session.delete(person);
	}
	public void edit(Person person) {		
		Session session = sessionFactory.getCurrentSession();
		Person existingPerson = (Person) session.get(Person.class, person.getId());
		existingPerson.setFirstName(person.getFirstName());
		existingPerson.setLastName(person.getLastName());
		existingPerson.setMoney(person.getMoney());
		session.save(existingPerson);
	}
}
